import { Notification } from 'element-ui';
import { orderBy } from 'lodash';
import { message } from '@/utils/util';
import { errorHandler, parseToObject } from '@/utils';
import { BATCHING_ADUIT_LIST, GOODS_STATUS_TYPES } from '@/constants';
import { exportGoodsPriceApi } from '@/views/audit-price/components/EditModal/api';
import { reAuditByAuditGoodsIdsApi } from '@/views/audit-price-recheck/api';
import { INITIAL_FORM_QUERY_VALUE } from '@/views/audit-price-recheck/constants';

export default {
  name: 'list-table',
  props: {
    data: {
      type: Array,
      default: () => [],
    },
    total: {
      type: Number,
      default: 0,
    },
    pageNum: {
      type: Number,
      default: 1,
    },
    pageSize: {
      type: Number,
      default: 100,
    },
    loading: {
      type: Boolean,
      default: false,
    },
    // 当前选择的查询条件
    currFormValueString: {
      id: String,
      default: null,
    },
  },
  computed: {
    currentPage() {
      return this.pageNum;
    },
    currentPageSize() {
      return this.pageSize;
    },
    // 所有的table字段
    bindTableColumns() {
      return this.batchingAuditList;
    },
    // 是否全选【展示table的字段】
    checkAll() {
      return this.allCheckedCols.length === this.batchingAuditList.length;
    },
    // 选中的需要展示的table字段
    checkedTableColumns() {
      const obj = this.batchingAuditList.reduce((prev, curr, index) => {
        return { ...prev, [curr.prop]: { ...curr, index } };
      }, {});
      return orderBy(
        this.allCheckedCols.map(item => ({ ...obj[item] })),
        ['index'],
        ['asc'],
      );
    },
  },
  data() {
    return {
      formQueryValue: { ...INITIAL_FORM_QUERY_VALUE }, // 当前选择的查询条件

      exporting: false, // 导出信息的按钮loading

      batchingAuditList: [...BATCHING_ADUIT_LIST], // 筛选字段列表
      allCheckedCols: [...BATCHING_ADUIT_LIST].filter(item => item.disabled).map(item => item.prop), // 选中的筛选字段

      visible: false, // 批量审核弹窗
      formModel: {}, // 批量审核弹窗表单
      successLoading: false, // 审核通过loading
      rebutLoading: false, // 复审驳回loading
    };
  },
  watch: {
    currFormValueString(newValue) {
      if (!newValue) {
        return;
      }
      this.formQueryValue = { ...parseToObject(newValue) };
    },
  },
  methods: {
    onPageSizeChange(pageSize) {
      this.$emit('pageSizeChange', pageSize);
    },
    onPageNumChange(pageNum) {
      this.$emit('pageNumChange', pageNum);
    },
    // 导出数据
    async exportGoodsPrice() {
      try {
        this.exporting = true;
        const res = await exportGoodsPriceApi({ ...this.formQueryValue });
        if (!res) {
          Notification({ type: 'success', title: '提示', message: '导出成功！请打开下载任务页面获取数据' });
        }
        this.exporting = false;
      } catch (error) {
        this.exporting = false;
        errorHandler({ error });
      }
    },
    // 点击【批量审核】
    async onBatchAudit() {
      let selectdRow = this.$refs.goodsTable.selection.length === 0 ? null : this.$refs.goodsTable.selection;
      if (!selectdRow) {
        message('提示', '请选择商品记录', 'warning');
        return;
      }
      this.visible = true;
    },
    onClose() {
      this.visible = false;
      this.formModel = {};
      this.successLoading = false;
      this.rebutLoading = false;
    },
    // 审核通过/复审驳回
    async onAudit(status) {
      this.successLoading = status === 'SUCCESS';
      this.rebutLoading = status === 'REBUT';
      const auditGoodsIds = (this.$refs.goodsTable.selection || []).map(item => item.id);
      try {
        await reAuditByAuditGoodsIdsApi({
          auditGoodsIds,
          remark: this.formModel.remark || '',
          status,
        });
        message('提示', '操作成功', 'success');
        this.onClose();
        this.$emit('resetTable');
      } catch (e) {
        this.successLoading = false;
        this.rebutLoading = false;
        message('提示', '批量审核失败', 'error');
      }
    },
    // 根据接口返回的status获取对应的文案展示
    formatStatusText(status) {
      if (!status) {
        return '';
      }

      let statusText = '';
      const filterList = GOODS_STATUS_TYPES.filter(item => item.id === status) || [];
      if (filterList.length > 0) {
        statusText = filterList[0].name || '';
      }
      return statusText;
    },
    // 全选
    handleCheckAllChange(val) {
      this.allCheckedCols = this.batchingAuditList.filter(item => (item.disabled ? true : !!val)).map(item => item.prop);
    },
  },
  mounted() {},
};
